﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ciatel.ISO8583.Amex;
using System.Net;
using ESF.SSO.Core.Entities;
using System.ServiceModel;
using ESF.General;
using ESF.SSO.Core.Entities.Exceptions;
using ESF.General.Entity;

#endregion


namespace ESF.SSO.ISO8583.ServerRequestHandler.Operations
{
	internal static class GetUserMenuImages
	{
		internal static void ProcessRequest (List<Message> pRequests, IPEndPoint pSenderIPEndPoint, IPEndPoint pForwarderIPEndPoint, out List<Message> pResponses, out List<object> pProcessedObjects)
		{
			Message response;
			
			Message request;
			
			CustomField customField_57;
			
			List<FileReference> files;
			
			string sessionID;
			
			int tmp, indiceImagen, indicePagina;

			
			request = pRequests [0];
			
			request.ApplicationData.DataElements.CheckHasMandatoryFields
			(
				TheKnownFields.CustomPrivateField_57
			);
			
			customField_57 = request.ApplicationData.DataElements.GetCustomPrivateField_57 ();
			
			pResponses = new List<Message> ();
			
			
			try
			{
				sessionID = customField_57.GetStringFieldWithVariableLength (36); // Ans .. 36
			
				using (SSOServiceClient.SSOServiceClient ssoClient = new SSOServiceClient.SSOServiceClient ())
				{
					files = ssoClient.GetUserMenuImages (new User () { SessionID = sessionID }, new BasicReference () { Code = customField_57.GetStringFieldWithVariableLength (60) });
				}

				indiceImagen = customField_57.GetNumericFieldAsInt (4);	// N 4
				indicePagina = customField_57.GetNumericFieldAsInt (4); // N 4
				
				indiceImagen -= 1;
				indicePagina -= 1;

				if (indiceImagen < 0) { indiceImagen = 0; }
				if (indicePagina < 0) { indicePagina = 0; }


				#region Partes de Archivo
				
				int index, difLength;


				Console.Write ("Arrancando desde: {0}-{1}", indiceImagen, indicePagina);

				//for (; indiceImagen < files.Count; indiceImagen ++)
				//{
					tmp = (int) Math.Ceiling (files [indiceImagen].String64Contents.Length / 700.0);

					Log.WriteLineHistory ("Imagen {0}: La imagen {1} tiene una longitud string-64 de {2} y partida por 700 da {3} pedazos, enviando el pedazo {4}", 
						indiceImagen + 1, files [indiceImagen].URL, files [indiceImagen].String64Contents.Length, tmp, indicePagina);

					index = indicePagina * 700;

                    //for (int j = indicePagina; j <= tmp; j++)
					//{
                        response = request.GenerateBasicResponse(pForwarderIPEndPoint.Port);

                        customField_57 = new CustomField();

						// SessionID
						customField_57.AddFieldWithVariableLength (sessionID, 40);				// Ans .. 40
				
						// Total de imagenes
						if (files != null)
						{
							customField_57.AddField (files.Count, 4);	// N 4
						}
						else
						{
							customField_57.AddField (0, 4);	// N 4
						}
				
						// Indice de Imagen
				        customField_57.AddField(indiceImagen + 1, 4);	// N 4

						// Total de paginas de la imagen
                        customField_57.AddField(tmp, 4); // N 4

						// Partes
						customField_57.AddField (indicePagina + 1, 4);	// N 4
						
						customField_57.AddFieldWithVariableLength (files [indiceImagen].asoBasicReferenceItem.Code, 60);	// Ans .. 60
						
						difLength = files [indiceImagen].String64Contents.Length - (index + 700);

						if (difLength < 0)
						{
							difLength = 700 + difLength;
						}
						else
						{
							difLength = 700;
						}
						
						customField_57.AddFieldWithVariableLength (files [indiceImagen].String64Contents.Substring (index, difLength), 700); // Ans .. 700
						
						customField_57.AddFieldWithVariableLength (System.IO.Path.GetExtension (files [indiceImagen].URL), 60);	// Ans .. 60
						
						if (files [indiceImagen].FileType.Code.StartsWith ("Icon"))
						{
							customField_57.AddField (1, 2); // N 2
						}
						else
						{
							customField_57.AddField (2, 2); // N 2
						}

                        response.ApplicationData.DataElements.SetCustomPrivateField_57(customField_57);

						pResponses.Add (response);

						//index += difLength;
					//}

					//i ++;
				//}
				
				#endregion
				
				pProcessedObjects = new List<object> ();
				
				pProcessedObjects.Add (files);
			}
			catch (FaultException gEx)
			{
				throw new GenericException (SSOExceptionCodes.ToSSOISO8583ExceptionCode (gEx.Code.Name), gEx.Message);
			}			
		}
	}
}
